//
//  AppDelegate.m
//  System_App
//
//  Created by czy001 on 2017/11/27.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "AppDelegate.h"
#import "SYS_LoginVC.h"
#import "AppDelegate+EaseMob.h"
#import "AppDelegate+Parse.h"
#import "SYS_LoginModelHelp.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _connectionState = EMConnectionConnected;
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //获取设备信息
    [self getEquipmentInformation];
    
    /*** 加载当前登录信息 **/
    [SYS_LoginModelHelp initUserInfo];
    
    //存储手机唯一标识码
    UICKeyChainStore *keychainStore = [UICKeyChainStore keyChainStore];
    NSString *pid = [keychainStore stringForKey:@"pid"];
    if (pid.length == 0) {
        //保存一个UUID字符串到钥匙串：
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        assert(uuid != NULL);
        CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
        
        pid=[[NSString stringWithFormat:@"%@", uuidStr] stringByReplacingOccurrencesOfString:@"-" withString:@""];
        
        [keychainStore setString:pid forKey:@"pid"];//存储手机唯一标识码
        // keychainStore[@"passwordabc"] = @"abcd1234";//存储手机唯一标识码，跟上面的使用方法是一样的效果
    }
    //缓存pid
    [[NSUserDefaults standardUserDefaults] setObject:pid forKey:@"PID"];
    
    if ([UIDevice currentDevice].systemVersion.floatValue >= 7.0) {
        [[UINavigationBar appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:RGBACOLOR(245, 245, 245, 1), NSForegroundColorAttributeName, [UIFont fontWithName:@ "HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
    }
    
    // 环信UIdemo中有用到Parse，您的项目中不需要添加，可忽略此处。
    [self parseApplication:application didFinishLaunchingWithOptions:launchOptions];
    

#warning 初始化环信SDK，详细内容在AppDelegate+EaseMob.m 文件中
#warning SDK注册 APNS文件的名字, 需要与后台上传证书时的名字一一对应
    
    //正式
    NSString *path=[[NSBundle mainBundle] pathForResource:@"HXDev" ofType:@"plist"];
    NSDictionary *hxDic = [[NSDictionary alloc] initWithContentsOfFile:path];
    //正式
    NSString *apnsCertName = [hxDic objectForKey:@"apnsDevName"];
    NSString *appkey = [hxDic objectForKey:@"appkey"];
    
    [self easemobApplication:application
didFinishLaunchingWithOptions:launchOptions
                      appkey:appkey
                apnsCertName:apnsCertName
                 otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
    
    // [1]:使用APPID/APPKEY/APPSECRENT创建个推实例
    // 该方法需要在主线程中调用
    [self startSdkWith:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret];
    
    _operation=[[DSDataBaseOperation alloc] init];
    [_operation CreateTable];
    
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)getEquipmentInformation{
    
    //设备别名
    NSString * iponeName =[DSEquipmentInformation getiPhoneName];
    
    //获取app名称
    NSString * aPPName =[DSEquipmentInformation getAPPName];
    
    //获取app版本号
    NSString * aPPVerion =[DSEquipmentInformation getAPPVerion];
    
    //当前系统名称
    NSString * systemName =[DSEquipmentInformation getSystemName];
    
    //当前系统版本号
    NSString * systemVersion =[DSEquipmentInformation getSystemVersion];
    
    //设备型号
    NSString * deviceName =[DSEquipmentInformation getDeviceName];
    
    //手机所属运营商
    NSString * carrierName =[DSEquipmentInformation getCarrierName];
    
    DSLog(@"++++++%@+++++++%@++++++%@+++++%@+++++++%@++++++%@+++++%@",iponeName,aPPName,aPPVerion,systemName,systemVersion,deviceName,carrierName);
}



#pragma mark - 启动GeTuiSdk
- (void)startSdkWith:(NSString *)appID appKey:(NSString *)appKey appSecret:(NSString *)appSecret {
    //[1-1]:通过 AppId、 appKey 、appSecret 启动SDK
    //该方法需要在主线程中调用
    [GeTuiSdk startSdkWithAppId:appID appKey:appKey appSecret:appSecret delegate:self];
    
    //[1-2]:设置是否后台运行开关
    [GeTuiSdk runBackgroundEnable:YES];
    //    //[1-3]:设置电子围栏功能，开启LBS定位服务 和 是否允许SDK 弹出用户定位请求
    //    [GeTuiSdk lbsLocationEnable:YES andUserVerify:YES];
}


//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
//{
//    if (_mainController) {
//        [_mainController jumpToChatList];
//    }
//}
//
//- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
//{
//    if (_mainController) {
//        [_mainController didReceiveLocalNotification:notification];
//    }
//}

#pragma mark - background fetch  唤醒
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    //[5] Background Fetch 恢复SDK 运行
    [GeTuiSdk resume];
    
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
